package com.dao;

import com.entity.TAuthor;
import com.entity.TAuthorExample;
import com.mapper.TAuthorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by del on 16-5-1.
 */
@Component
public class TAuthorDao {

    @Autowired
    private TAuthorMapper tAuthorMapper;

    public boolean insert(TAuthor tAuthor){
        return tAuthorMapper.insert(tAuthor) == 1;
    }

    public boolean delete(Integer id){
        return tAuthorMapper.deleteByPrimaryKey(id) == 1;
    }

    public TAuthor update(TAuthor tAuthor){
        return tAuthorMapper.updateByPrimaryKey(tAuthor) == 1 ? tAuthor : null;
    }

    public TAuthor select(Integer id){
        return tAuthorMapper.selectByPrimaryKey(id);
    }

    public TAuthor selectByUid(String uid) {
        TAuthorExample tAuthorExample = new TAuthorExample();
        tAuthorExample.createCriteria()
                .andUidEqualTo(uid);
        List<TAuthor> tAuthorList = tAuthorMapper.selectByExample(tAuthorExample);
        if (!tAuthorList.isEmpty()) {
            return tAuthorMapper.selectByExample(tAuthorExample).get(0);
        }
        else {
            return null;
        }
    }

    public List<TAuthor> selectByExample(TAuthorExample tAuthorExample) {
        return tAuthorMapper.selectByExample(tAuthorExample);
    }

    public TAuthor selectNewest() {
        return tAuthorMapper.selectNewest();
    }
}
